package com.taomu.app.zongchouhuli.moudle.mine.activity;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.http.res.ResMyBuyProductEntity;
import com.taomu.app.zongchouhuli.http.res.ResMyProductEntity;
import com.taomu.app.zongchouhuli.http.res.ResUserInfoEntity;
import com.taomu.app.zongchouhuli.moudle.mine.adpter.RebateListAdp;
import com.taomu.app.zongchouhuli.moudle.mine.contract.UserRebateListContract;
import com.taomu.app.zongchouhuli.moudle.mine.contract.UserRechargeContract;
import com.taomu.app.zongchouhuli.moudle.mine.contract.impl.UserRebateListPresenter;
import com.taomu.app.zongchouhuli.moudle.mine.entity.PayWayEntity;
import com.taomu.app.zongchouhuli.moudle.mine.entity.RebateListEntity;
import com.taomu.app.zongchouhuli.utils.ListViewUtils;
import com.taomu.app.zongchouhuli.utils.ParseUtil;
import com.taomu.app.zongchouhuli.utils.StorageHelper;
import com.taomu.app.zongchouhuli.weight.EmptyRecyclerView;

import java.util.List;

import static com.taomu.app.zongchouhuli.moudle.mine.activity.UserRebateDetailActivity.REBATEPRODUCTID;
import static com.taomu.app.zongchouhuli.moudle.mine.activity.UserRebateDetailActivity.REBATEPRODUCTINFO;

public class UserRebateListActivity extends BaseActivity<UserRebateListContract.Presenter> implements UserRebateListContract.View {

    private ImageView mBackIm;

    //每日返利
    private TextView mDayRebateTv;

    //总返利
    private TextView mTotalRebateTv;

    private EmptyRecyclerView mRebateRecy;

    private RebateListAdp mRebateListAdp;

    private SmartRefreshLayout mRefreshLayout;

    private int ship = 0;

    private int take = 10;

    private String product_status = "";

    private TextView mRebateTv;

    private TextView mCurrentMoneyTv;

    @Override
    protected void onDestroy() {
        mPresenter.detch();
        super.onDestroy();
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_rebate_list;
    }

    @Override
    protected void initView() {
        mRefreshLayout = findViewById(R.id.rebate_list_refresh);
        mBackIm = findViewById(R.id.rebate_list_back_im);
        mCurrentMoneyTv = findViewById(R.id.rebate_list_current_income_tv);
        mRebateTv = findViewById(R.id.rebate_zhuanru_tv);
        mDayRebateTv = findViewById(R.id.rebate_list_income_tv);
        mTotalRebateTv = findViewById(R.id.rebate_list_total_income_tv);
        mRebateListAdp = new RebateListAdp(this);
        mRebateRecy = findViewById(R.id.rebate_list_recy);
        mRebateRecy.setLayoutManager(new LinearLayoutManager(this));
        mRebateRecy.setNestedScrollingEnabled(false);
        mRebateRecy.setAdapter(mRebateListAdp);
        ListViewUtils.setDefaultEmpty(mRebateRecy);
    }

    private String currentTotalMoney;

    @Override
    protected void initData() {
        currentTotalMoney = ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getProduct_account();
        if(TextUtils.isEmpty(currentTotalMoney) || "null".equals(currentTotalMoney)){
            totalMoney = 0;
        }else{
            totalMoney = Double.valueOf(currentTotalMoney);
        }
        mCurrentMoneyTv.setText("¥"+currentTotalMoney);
        //mPresenter.getRebateList();
    }

    @Override
    protected void onResume() {
        mRefreshLayout.autoRefresh();
        super.onResume();
    }

    @Override
    protected void initEvent() {
        mRebateListAdp.setRebateListListener(new RebateListAdp.RebateListListener() {
            @Override
            public void onItemClick(ResMyBuyProductEntity.DataBean.ListBean entity) {
                Intent intent = new Intent(UserRebateListActivity.this, UserRebateDetailActivity.class);
                intent.putExtra(REBATEPRODUCTID,String.valueOf(entity.getId()));
                intent.putExtra(REBATEPRODUCTINFO, entity);
                startActivity(intent);
            }
        });
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRefreshLayout.setOnRefreshListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                mPresenter.getRebateList(ship,take);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mRefreshLayout.setEnableLoadMore(true);
                ship = 0;
                mPresenter.getRebateList(ship,take);
            }
        });
        mRefreshLayout.setOnLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                mPresenter.getRebateList(ship,take);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {

            }
        });
        mRebateTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
    }

    @Override
    protected UserRebateListContract.Presenter createPresenter() {
        return new UserRebateListPresenter(this);
    }

    @Override
    public void setRebateList(List<RebateListEntity> mData) {
        //mRebateListAdp.setData(mData);
    }

    private double totalMoney = 0;

    @Override
    public void setRebateData(ResMyBuyProductEntity mData) {
        if(mData.getData() != null) {
            mDayRebateTv.setText("¥" + String.valueOf(mData.getData().getProductIncomeToday()));
            mTotalRebateTv.setText("¥" + String.valueOf(mData.getData().getProductIncome()));
            //totalMoney = mData.getData().getProductIncome();
            if (mRefreshLayout.isLoading()) {
                mRebateListAdp.addData(mData.getData().getList());
            } else if (mRefreshLayout.isRefreshing()) {
                mRebateListAdp.setData(mData.getData().getList());
            } else {
                mRebateListAdp.setData(mData.getData().getList());
            }
            ship = mRebateListAdp.getItemCount();
            if(Integer.valueOf(mData.getCount())>mRebateListAdp.getItemCount()){
                mRefreshLayout.setEnableLoadMore(true);
            }else{
                mRefreshLayout.setEnableLoadMore(false);
            }
        }else{
            mRefreshLayout.setEnableLoadMore(false);
        }
    }

    @Override
    public void setMsg(String msg) {
        mPresenter.getUserInfo();
        showToast(msg);
    }

    @Override
    public void setTotalMoney(String money) {
        if(TextUtils.isEmpty(money) || "null".equals(money)){
            totalMoney = 0;
        }else{
            totalMoney = Double.valueOf(currentTotalMoney);
        }
        mCurrentMoneyTv.setText("¥"+money);
    }

    @Override
    public void setPresenter(UserRebateListContract.Presenter presenter) {

    }

    @Override
    public void dismissLoadingDialog() {
        super.dismissLoadingDialog();
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
    }

    private Dialog dialog;

    private void showDialog(){
        if(dialog == null){
            dialog = new Dialog(this);
            View view = LayoutInflater.from(this).inflate(R.layout.dialog_zhuanru_layout, null);
            final EditText mMonyEt = view.findViewById(R.id.zhuanru_money_et);
            final TextView cancleTv = view.findViewById(R.id.zhuanru_cancle_tv);
            final TextView confirmTv = view.findViewById(R.id.zhuanru_confirm_tv);
            final ImageView mClose = view.findViewById(R.id.btn_close);
            mClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mMonyEt.setText("");
                    if(dialog != null){
                        dialog.dismiss();
                    }
                }
            });
            confirmTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String moneystr = mMonyEt.getText().toString();
                    if(!TextUtils.isEmpty(moneystr)){
                        int money = Integer.valueOf(moneystr);
                        if(money == 0){
                            showToast("金额不能为0");
                            return;
                        }
                        if(totalMoney>=money){
                            mMonyEt.setText("");
                            mPresenter.tixian(moneystr);
                            if(dialog != null){
                                dialog.dismiss();
                            }
                        }else{
                            showToast("输入金额不能大于产品总收益");
                        }

                    }else{
                        showToast("请输入金额");
                    }
                }
            });
            cancleTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mMonyEt.setText("");
                    if(dialog != null){
                        dialog.dismiss();
                    }
                }
            });
            dialog.setContentView(view);
            dialog.show();
        }else{
            dialog.show();
        }
    }
}
